import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';
import { JwtTokenDTO } from '../../model/token/jwt-token.dto';
import { UserLoginInfoDTO } from '../../model/token/user-login-info-dto';

@Injectable({
  providedIn: 'root',
})
export class HttpTokenService {
  /**
   * API的入口
   */
  private static URL = '/api/token';

  constructor(private http: _HttpClient) {}

  /**
   * 登录
   * @param username 用户名
   * @param password 密码
   * @param rememberMe 是否记住，决定token有效期
   */
  login(username: string, password: string, rememberMe: number): Observable<JwtTokenDTO> {
    return this.http.post<JwtTokenDTO>(
      `${HttpTokenService.URL}?_allow_anonymous=true`,
      new UserLoginInfoDTO(username, password, rememberMe),
    );
  }
}
